/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aqlm.util;

import com.aqlm.bizexcp.NoSuchPropertyRuntimeException;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 *
 * @author hironaga
 */
public class PropertiesUtil {
    
    /**
     * propeties file
     */
    private static Properties xFilePathProp;
    private static Properties xPathProp;
    
    //log
    private static Logger log = Logger.getLogger(PropertiesUtil.class);
    
    //to limit "new"
    private PropertiesUtil(){}
    
    /**
     * **Class Constructor**
     * read properties file
     */
    static {
        try{
            //XFilePath.properties
            InputStream isFPath = PropertiesUtil.class.getResourceAsStream("/XFilePath.properties");
            xFilePathProp = new Properties();
            xFilePathProp.load(isFPath);
            isFPath.close();
        
            //XPath.properties
            InputStream isXPath = PropertiesUtil.class.getResourceAsStream("/XPath.properties");
            xPathProp = new Properties();
            xPathProp.load(isXPath);
            isXPath.close();
            
        }
        catch(IOException ioe){
            log.fatal(".properties 読み込み失敗", ioe);
        }
    }//static
    
    public static String getXFilePath(String key) throws NoSuchPropertyRuntimeException{
        String value = xFilePathProp.getProperty(key);
        if(value == null || value.length() == 0){
            log.error("key=" + key);
            throw new NoSuchPropertyRuntimeException();
        }
        return value;
    }
    
    public static String getXFilePath(String key, Object[] params) throws NoSuchPropertyRuntimeException{
        String value = xFilePathProp.getProperty(key);
        if(value == null || value.length() == 0){
            log.error("key=" + key);
            throw new NoSuchPropertyRuntimeException();
        }
        MessageFormat format = new MessageFormat(value);
        return format.format(params).toString();
    }
    
    public static String getXPath(String key) throws NoSuchPropertyRuntimeException{
        String value = xPathProp.getProperty(key);
        if(value == null || value.length() == 0){
            log.error("key=" + key);
            throw new NoSuchPropertyRuntimeException();
        }
        return value;
    }
    
    public static String getXPath(String key, Object[] params) throws NoSuchPropertyRuntimeException{
        String value = xPathProp.getProperty(key);
        if(value == null || value.length() == 0){
            log.error("key=" + key);
            throw new NoSuchPropertyRuntimeException();
        }
        MessageFormat format = new MessageFormat(value);
        return format.format(params).toString();
    }
    
}
